from rest_framework.generics import ListAPIView, ListCreateAPIView

from meiduo_admin.serializers.users import UserSerializer
from meiduo_admin.utils import PageNum
from users.models import User


class UserView(ListCreateAPIView):

    pagination_class = PageNum
    # 重写获取查询集数据的方法
    # queryset = User.objects.all()
    serializer_class = UserSerializer
    # def get_serializer_class(self):
    #
    #     if self.request.method == 'get':
    #         return UserSerializer
    #     else:
    #         # POST请求，完成保存用户，返回UserAddSerializer
    #         return UserCreateSerializer

    def get_queryset(self):
        if self.request.query_params.get('keyword') == '' or None:
            return User.objects.all()

        else:
            return User.objects.filter(username__contains=self.request.query_params.get('keyword'))